var t = getApp();

Page({
  data: {
    typeArr: [

    ],
    type: '请选择',
    textareaHeight: 250,
    currenttype: 1,
    saidtype: 1,
    patientIdArr: [],
    teamList: [],
    monentIdea: "",
    monentIdeaText: "",
    imgArr: [],
    imgArr2: [],
    imgPath: "",
    imgList: [],
    showFlag: !1,
    propagandaId: "",
    sendFalg: !0,
    gotoUrl: "/hyb_yl/backstageFollowUp/pages/ChangePatient/ChangePatient",
    time: null,
    data_arr: []
  },

  bindType: function(e) {
    var that = this;
    let typeArr2 = that.data.typeArr2;
    that.setData({
      type: that.data.typeArr[e.detail.value],
      hj_id: typeArr2[e.detail.value].hj_id
    })

  },
  onLoad: function(options) {
    console.log(this)
    var e = this;
    var zid = options.zid
    var z_name = options.z_name
    var uniacid = t.siteInfo.uniacid;
    e.setData({
      zid: zid,
      uniacid: uniacid,
      z_name: z_name
    })
    t.util.request({
      'url': 'entry/wxapp/url',
      success: function(res) {
        console.log(res)
        e.setData({
          url: res.data
        })
      },
    })
    wx.getRecorderManager && (a.onStart(function() {
        console.log("recorder start"), e.setData({
          voiceFlag: !0
        }), e.data.flagBoo && a.stop();
      }),
      a.onStop(function(t) {
        var a = t.tempFilePath,
          n = (t.duration / 1e3).toFixed();
        n <= 0 ? wx.showToast({
          title: "太短了",
          icon: "none",
          duration: 500
        }) : (e.setData({
          time: n,
          soundFlag: !0,
          tempFilePath: [a]
        }), console.log(n));
      })), wx.createInnerAudioContext && (n.onPlay(function(t) {
      console.log(t), console.log("开始播放"), e.setData({
        playFlag: !0
      });
    }), n.onError(function(t) {}), n.onStop(function() {
      e.setData({
        playFlag: !1
      });
    }), n.onEnded(function() {
      console.log("播放完毕了"), e.setData({
        playFlag: !1
      });
    }));
  },
  touxiang: function() {
    var that = this;
    var uniacid = t.siteInfo.uniacid;
    var imgss = that.data.imgs;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // console.log(res)
        var tempFilePaths = res.tempFilePaths[0];
        //console.log(tempFilePaths);
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadsarray&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function(res) {
            console.log(res.data)
            that.setData({
              uplogo: res.data,
              touxiangurl: tempFilePaths,
              touxiang: false
            })
          },
          fail: function(res) {
            console.log(res)
          },
        })
        that.setData({
          logo: tempFilePaths
        })
      }
    })
  },
  onReady: function() {
    this.getAllhjfenl()
  },
  onShow: function() {
    var t = this;
    if (wx.getSetting({
        success: function(a) {
          0 == a.authSetting["scope.record"] && t.setData({
            recordBoo: !0
          }), null == a.authSetting["scope.record"] && t.setData({
            recordBooTwo: !0
          }), a.authSetting["scope.record"] && t.setData({
            recordBoo: !1
          });
        }
      }), this.setData({
        patientList: wx.getStorageSync("patientList") || "",
        token: wx.getStorageSync("log"),
        currentSaid: wx.getStorageSync("currentSaid") || ""
      }), !this.data.showFlag) {
      if ("" == this.data.patientList) this.setData({
        patientFlag: !1
      });
      else {
        if (1 == this.data.patientType) {
          var a = 0,
            e = 0,
            n = [],
            o = [];
          null != this.data.patientList.groups && this.data.patientList.groups.length > 0 && this.data.patientList.groups.forEach(function(t) {
            t.groupCheck && null != t.patients && t.patients.length > 0 && a++, null != t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
              t.patientCheck && (n.push(t.userId), e++);
            });
          }), null != this.data.patientList.unGroupPatientList && this.data.patientList.unGroupPatientList.length > 0 && this.data.patientList.unGroupPatientList.forEach(function(t) {
            t.patientCheck && (n.push(t.userId), e++);
          }), console.log(a), console.log(e), console.log(n), 0 == a && 0 == e && n.length <= 0 ? this.setData({
            patientFlag: !1
          }) : this.setData({
            patientFlag: !0,
            groupNum: a,
            totalNum: e,
            patientIdArr: n,
            teamList: []
          });
        }
        if (2 == this.data.patientType) {
          var a = 0,
            e = 0,
            n = [],
            o = [],
            i = [];
          null != this.data.patientList.groups && this.data.patientList.groups.length > 0 && this.data.patientList.groups.forEach(function(t) {
            t.groupCheck && null != t.patients && t.patients.length > 0 && a++, null != t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
              t.patientCheck && (i.push(t.userId), e++);
            });
          }), null != this.data.patientList.unGroupPatientList && this.data.patientList.unGroupPatientList.length > 0 && this.data.patientList.unGroupPatientList.forEach(function(t) {
            t.patientCheck && (i.push(t.userId), e++);
          }), console.log(a), console.log(e), console.log(i), 0 == a && 0 == e && i.length <= 0 ? this.setData({
            patientFlag: !1
          }) : (o.push({
            teamId: this.data.teamId,
            receiveIds: i
          }), this.setData({
            patientFlag: !0,
            groupNum: a,
            totalNum: e,
            patientIdArr: n,
            teamList: o
          }));
        }
        if (3 == this.data.patientType) {
          var a = 0,
            e = 0,
            n = [],
            o = [],
            i = [];
          this.data.patientList.teams.forEach(function(t, s) {
            0 == s && (t.groups.length > 0 && t.groups.forEach(function(t) {
              t.groupCheck && null != t.patients && t.patients.length > 0 && a++, null != t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
                t.patientCheck && (n.push(t.userId), e++);
              });
            }), t.unGroupPatients.length > 0 && t.unGroupPatients.forEach(function(t) {
              t.patientCheck && (n.push(t.userId), e++);
            })), s > 0 && (i = [], t.groups.length > 0 && t.groups.forEach(function(t) {
              t.groupCheck && null != t.patients && t.patients.length > 0 && a++, null != t.patients && t.patients.length > 0 && t.patients.forEach(function(t) {
                t.patientCheck && (i.push(t.userId), e++);
              });
            }), t.unGroupPatients.length > 0 && t.unGroupPatients.forEach(function(t) {
              t.patientCheck && (i.push(t.userId), e++);
            }), i.length > 0 && o.push({
              teamId: t.teamId,
              receiveIds: i
            }));
          }), console.log(a), console.log(e), console.log(n), 0 == a && 0 == e && n.length <= 0 ? this.setData({
            patientFlag: !1
          }) : this.setData({
            patientFlag: !0,
            groupNum: a,
            totalNum: e,
            patientIdArr: n,
            teamList: o
          });
        }
      }
      this.data.currentSaid && (this.data.propagandaId = this.data.currentSaid.eduId ? this.data.currentSaid.eduId : this.data.currentSaid.id);
    }
    this.setData({
      showFlag: !1
    });
  },
  onHide: function() {},
  onUnload: function() {
    2 == this.data.fromType && wx.removeStorageSync("patientList"), wx.removeStorageSync("currentSaid");
  },
  tapSaidTextType: function(t) {
    console.log(t), t.target.dataset.currenttype != this.data.currenttype && (1 == t.target.dataset.currenttype && n.stop(),
      this.setData({
        currenttype: t.target.dataset.currenttype,
        soundFlag: !1,
        tempFilePath: [],
        time: null,
        monentIdeaText: ""
      }));
  },
  touchstart: function(t) {
    var n = this;
    a ? (a.start(e), this.data.flagBoo = !1, this.data.recordBoo && (wx.showModal({
      title: "提示",
      content: "您暂未授权录音功能，请打开右上角 -> 关于惠邦肾 -> 右上角 -> 设置中授权录音功能",
      showCancel: !1,
      confirmColor: "#3b4ca9",
      confirmText: "我知道了"
    }), a.stop()), this.data.recordBooTwo && (wx.getSetting({
      success: function(t) {
        console.log(t), 0 == t.authSetting["scope.record"] && (wx.showModal({
          title: "提示",
          content: "您暂未授权录音功能，请打开右上角 -> 关于惠邦肾 -> 右上角 -> 设置中授权录音功能",
          showCancel: !1,
          confirmColor: "#3b4ca9",
          confirmText: "我知道了"
        }), n.setData({
          recordBoo: !0,
          recordBooTwo: !1
        }));
      }
    }), a.stop())) : wx.showModal({
      content: "您当前微信版本过低，暂不支持语音功能，请更新微信版本后重试",
      showCancel: !1,
      confirmColor: "#3b4ca9",
      confirmText: "我知道了"
    });
  },
  touchend: function(t) {
    a && (this.data.flagBoo = !0, a.stop(), this.setData({
      voiceFlag: !1
    }));
  },
  againBtn: function() {
    this.setData({
      soundFlag: !1,
      tempFilePath: [],
      time: null
    });
  },
  toPlay: function() {
    n ? (n.src = this.data.tempFilePath[0], this.data.playFlag ? n.stop() : n.play()) : wx.showModal({
      content: "您当前微信版本过低，暂不支持语音功能，请更新微信版本后重试",
      showCancel: !1,
      confirmColor: "#3b4ca9",
      confirmText: "我知道了"
    });
  },
  tapSaidType: function(t) {
    t.target.dataset.saidtype != this.data.saidtype && (this.setData({
      saidtype: t.target.dataset.saidtype
    }), 2 == this.data.saidtype ? this.setData({
      imgArr: []
    }) : (this.setData({
      propagandaId: "",
      currentSaid: ""
    }), wx.removeStorageSync("currentSaid")));
  },
  getText: function(t) {
    this.setData({
      monentIdeaText: t.detail.value
    });
  },
  bindlinechange: function(t) {
    console.log(t), t.detail.heightRpx > 250 ? this.setData({
      textareaHeight: t.detail.heightRpx
    }) : this.setData({
      textareaHeight: 250
    });
  },
  chooseImage: function() {
    var t = this;
    wx.chooseImage({
      count: 9,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(a) {
        console.log(a);
        for (var e = a.tempFilePaths, n = 0; n < e.length && 9 != t.data.imgArr.length; n++) t.data.imgArr.push(e[n]);
        9 == t.data.imgArr.length && t.setData({
          addImgFlag: !0
        }), t.setData({
          imgArr: t.data.imgArr
        });
        console.log(t)
      }
    });
  },
  chooseVideo: function() {
    var t = this;
    wx.chooseVideo({
      count: 9,
      sizeType: ["original", "compressed"],
      maxDuration: 60,
      camera: 'back',
      compressed: false,
      success: function(a) {
        console.log(a);
        console.log(t)
      }
    });
  },


  chooseImage2: function() {
    var t = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function(a) {
        console.log(a);
        for (var e = a.tempFilePaths, n = 0; n < e.length && 9 != t.data.imgArr2.length; n++) t.data.imgArr2.push(e[n]);
        1 == t.data.imgArr2.length && t.setData({
          addImgFlag: !0
        }), t.setData({
          imgArr2: t.data.imgArr2
        });
        console.log(t)
      }
    });
    console.log(t)
  },
  delImage: function(t) {
    console.log(t), this.data.imgArr.splice(t.target.dataset.index, 1), this.data.imgArr.length < 9 && this.setData({
      addImgFlag: !1
    }), this.setData({
      imgArr: this.data.imgArr
    }), console.log(this.data.imgArr);
  },
  delImage2: function(t) {
    console.log(t)
    var that = this
    that.setData({
      touxiangurl: false
    })
  },
  previewImage: function(t) {
    console.log(t);
    var a = this;
    this.setData({
      showFlag: !0
    }), wx.previewImage({
      current: t.target.dataset.url,
      urls: a.data.imgArr
    });
  },

  publishBtn: function() {
    var t = this;
    if (1 == t.data.currenttype) {
      if (0 == t.data.monentIdeaText.replace(/(^\s*)|(\s*$)/g, "").length) return void wx.showToast({
        title: "请输入文字",
        icon: "none",
        duration: 2e3
      });
      this.data.monentIdea = this.data.monentIdeaText;
    }
    if (2 == t.data.currenttype && t.data.tempFilePath.length <= 0) wx.showToast({
      title: "请录制语音",
      icon: "none",
      duration: 2e3
    });
    else {
      if (1 == t.data.saidtype) {
        var a = this.data.imgArr,
          e = a.length,
          n = t.data.imgPath;
        if (wx.showLoading({
            title: "发表中...",
            mask: !0
          }), this.setData({
            sendFalg: !1
          }), e <= 0) return void(2 == this.data.currenttype ? t.sendVoice(n) : t.send(n));
        t.sendImg(a, 0, e);
      }
      if (2 == t.data.saidtype) {
        if (!t.data.propagandaId) return void wx.showToast({
          title: "请选择宣教",
          icon: "none",
          duration: 2e3
        });
        wx.showLoading({
          title: "发表中...",
          mask: !0
        }), t.setData({
          sendFalg: !1
        }), 2 == t.data.currenttype ? t.sendVoice(t.data.propagandaId) : t.send(t.data.propagandaId);
      }
    }
  },
  titles: function(e) {
    var that = this
    var hj_title = e.detail.value
    that.setData({
      hj_title: hj_title
    })
  },
  sendImg: function() {
    var that = this;
    var hj_tex = this.data.monentIdeaText
    var hj_id = that.data.hj_id
    var hj_title = that.data.hj_title
    var uplogo = that.data.uplogo
    var z_name = that.data.z_name
    if (that.data.imgArr) {
      var hj_type = 0
    }
    var data_arr = that.data.data_arr;
    console.log(data_arr)
    this.upload();
    wx.showToast({
      title: '上传中，请稍后',
      icon: "loading",
      duration: 1e3,
    })
    setTimeout(function() {
      t.util.request({
        'url': 'entry/wxapp/Savegerenhj',
        data: {
          zid: that.data.zid,
          hj_type: hj_type,
          hj_pic: data_arr,
          hj_tex: hj_tex,
          hj_id: hj_id,
          hj_title: hj_title,
          hj_thumb: uplogo,
          z_name: z_name,
          op: 'post'
        },
        success: function(res) {
          console.log(res)
          wx.showToast({
            title: '上传成功',
            icon: "none",
            duration: 1e3,
            success: function(res) {
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2];
              prevPage.setData({
                type: 1
              })
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            }
          })
        },
        fail: function(err) {
          console.log(err)
        },
      })
    }, 3000)

  },
  send: function(a) {
    var e = this;
    var hj_tex = e.data.monentIdea
    console.log(hj_tex)
    this.setData({
      hj_tex: hj_tex
    })

  },
  getAllhjfenl: function() {
    var that = this
    t.util.request({
      'url': 'entry/wxapp/Allhjfenl',
      data: {
        op: 'display'
      },
      success: function(res) {
        console.log(res)
        var typeArr = res.data.fenl;
        let arr = [];
        typeArr.map(function(i) {
          arr.push(i.hj_name)
        })
        var hjlist = res.data.hjlist
        that.setData({
          typeArr: arr,
          typeArr2: typeArr,
          hjlist: hjlist
        })
      }
    })
  },
  upload: function() {
    var that = this
    var uniacid = that.data.uniacid
    var data_arr = that.data.data_arr;
    console.log(data_arr)
    console.log(this.data.imgArr)
    for (var i = 0; i < this.data.imgArr.length; i++) {
      wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=hyb_yl',
        filePath: that.data.imgArr[i],
        name: 'upfile',
        formData: {},
        success: function(res) {
          console.log(res)
          data_arr.push(res.data)
          that.setData({
            data_arr: data_arr
          })
        }
      })
    }
  },
  sendVoice: function(a) {
    var e = this;
    console.log(e)
    wx.uploadFile({
      url: t.globalData.dic + "doctor/said/upload",
      method: "POST",
      header: {
        "content-type": "multipart/form-data"
      },
      filePath: this.data.tempFilePath[0],
      name: "file",
      formData: {
        token: e.data.token
      },
      success: function(t) {
        console.log(t), 200 == t.statusCode && (e.data.monentIdea = JSON.parse(t.data).data,
          e.send(a));
      }
    });
  }
});

var a = wx.getRecorderManager ? wx.getRecorderManager() : null,
  e = {
    sampleRate: 44100,
    numberOfChannels: 1,
    encodeBitRate: 192e3,
    format: "mp3",
    frameSize: 50
  },
  n = wx.createInnerAudioContext ? wx.createInnerAudioContext() : null;